package com.itheima.article.controller;


import com.itheima.article.dto.ArticleBehaviourDtoQuery;
import com.itheima.article.dto.ArticleInfoDto;
import com.itheima.article.pojo.ApArticle;
import com.itheima.article.service.ApArticleService;
import com.itheima.common.pojo.PageInfo;
import com.itheima.common.pojo.PageRequestDto;
import com.itheima.common.pojo.Result;
import com.itheima.core.controller.AbstractCoreController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
* <p>
* 文章信息表，存储已发布的文章 控制器</p>
* @author 作者
* @since 2021-08-20
*/
@RestController
@RequestMapping("/apArticle")
@CrossOrigin
public class ApArticleController extends AbstractCoreController<ApArticle> {

    private ApArticleService apArticleService;

    //注入
    @Autowired
    public ApArticleController(ApArticleService apArticleService) {
        super(apArticleService);
        this.apArticleService=apArticleService;
    }

    /**
     * 查询当前用户对当前文章操作的状态信息
     *  1:是否收藏
     *  2:是否已关注作者
     *  3:是否不喜欢
     *  4:是否点赞
     *  .......
     */
    @PostMapping(value = "/load/article/behavior")
    public Result loadArticleBehaviour(@RequestBody ArticleBehaviourDtoQuery dto){
        Map<String,Object> resultMap = apArticleService.loadArticleBehaviour(dto);
        return Result.ok(resultMap);
    }


    /***
     * 文章详情
     *  1)ApArticle
     *  2)ApArticleConfig
     *  3)ApArticleContent
     */
    @GetMapping(value = "/detail/{id}")
    public Result detailByArticleId(@PathVariable("id")Long id){
        if(id>1){
            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //执行查询
        ArticleInfoDto dto = apArticleService.detailByArticleId(id);
        return Result.ok(dto);
    }

    /***
     * 文章列表分页查询
     */
    @PostMapping(value = "/searchOrder")
    public Result searchOrder(@RequestBody PageRequestDto<ApArticle> dto){
        //查询数据->PageInfo<T>
        PageInfo<ApArticle> pageInfo = apArticleService.searchOrder(dto);
        return Result.ok(pageInfo);
    }

    /***
     * 增加
     *  1：ApArticle
     *  2：ApArticleContent
     *  3：APAritcleConfig
     */
    @PostMapping(value = "/articleInfo/save")
    public Result saveArticle(@RequestBody ArticleInfoDto dto){
        apArticleService.saveArticleInfo(dto);
        return Result.ok();
    }

}

